package cn.yr.search.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.yr.blog.api.ArticleApi;
import cn.yr.common.core.domain.dto.YrArticleTagDto;
import cn.yr.common.core.domain.entity.YrArticle;
import cn.yr.common.core.domain.vo.PageVo;
import cn.yr.common.exception.ret.Ret;
import cn.yr.search.bean.ArticleDocBean;
import cn.yr.search.service.ArticleElasticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author lang 1102076808@qq.com
 * @Description 
 * @Date 20:28 2021/6/6
 **/
@RestController
@RequestMapping("articleSearch")
public class ArticleSearchController {

    @Autowired
    private ArticleElasticService articleElasticService;

    @Autowired
    private ArticleApi articleApi;

    @GetMapping("initIndex")
    public Ret initIndex(){
        articleElasticService.deleteIndex();
        articleElasticService.createIndex();
        return Ret.success();
    }

    @GetMapping("initDoc")
    public Ret initDoc(){
        PageVo<YrArticleTagDto> pageVo = new PageVo<>();
        pageVo.setCurrent(1);
        pageVo.setSize(Integer.MAX_VALUE);
        Ret<PageVo<YrArticleTagDto>> pageVoRet = articleApi.pageArticle(pageVo);
        List<YrArticleTagDto> records = pageVoRet.getData().getRecords();
        ArrayList<ArticleDocBean> list = new ArrayList<>();
        records.forEach(record->{
            ArticleDocBean articleDocBean = new ArticleDocBean();
            BeanUtil.copyProperties(record,articleDocBean);
            list.add(articleDocBean);
        });
        articleElasticService.saveAll(list);
        return Ret.success();
    }

    @GetMapping("findAll")
    public Ret findAll(){
        Iterator<ArticleDocBean> all = articleElasticService.findAll();
        return Ret.success(all);
    }

    @GetMapping("pageArticleByTitle")
    public Ret pageArticleByTitle(@RequestBody PageVo<ArticleDocBean> pageVo){
        return Ret.success(articleElasticService.findByTitle(pageVo));
    }

    @GetMapping("pageArticle")
    public Ret pageArticle(@RequestBody PageVo<ArticleDocBean> pageVo){
        return Ret.success(articleElasticService.find(pageVo));
    }

    @PostMapping("addArticle")
    public Ret addArticle(@RequestBody YrArticle yrArticle){
        ArticleDocBean articleDocBean = new ArticleDocBean();
        BeanUtil.copyProperties(yrArticle,articleDocBean);
        articleElasticService.save(articleDocBean);
        return Ret.success();
    }

    @PostMapping("updateArticle")
    public Ret updateArticle(@RequestBody YrArticle yrArticle){
        ArticleDocBean articleDocBean = new ArticleDocBean();
        BeanUtil.copyProperties(yrArticle,articleDocBean);
        articleElasticService.update(articleDocBean);
        return Ret.success();
    }

    @DeleteMapping("updateArticle")
    public Ret deleteArticle(Long id){
        articleElasticService.delete(id);
        return Ret.success();
    }

}
